package com.yh.utils.extract.service.impl;

import com.yh.utils.extract.config.DynamicDatasource;
import com.yh.utils.extract.dao.DataSourceMapper;
import com.yh.utils.extract.model.DataSourceDO;
import com.yh.utils.extract.service.DataSourceService;
import com.yh.utils.extract.spring.listener.DbInitListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class DataSourceServiceImpl  implements DataSourceService {

    @Autowired
    DataSourceMapper dataSourceMapper;

    @Autowired(required = false)
    DynamicDatasource dynamicDatasource;
    @Autowired
    DbInitListener dbInitListener;

    @Override
    public List<DataSourceDO> queryList() {
        try {
            List<DataSourceDO> list = dataSourceMapper.queryList();
        }catch (Exception e){
           log.info("获取数据源失败");
        }finally {
            return queryList();
        }
//       return

    }

    @Override
    public DataSourceDO get(String id) {
        return this.dataSourceMapper.get(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(DataSourceDO dataSource) throws Exception{
        dataSource.setId(UUID.randomUUID().toString().replace("-",""));
        this.checkKey(dataSource);
        int i = this.dataSourceMapper.save(dataSource);
        if(i>0){
            boolean isSuccess = dynamicDatasource.createDataSource(dataSource);
            if(!isSuccess){
                throw new Exception("数据源错误");
            }
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(DataSourceDO dataSource) throws Exception {
        this.checkKey(dataSource);
        DataSourceDO dataSourceDO = this.get(dataSource.getId());
        int i = dataSourceMapper.updateByPrimaryKey(dataSource);
        if(i>0){
            boolean isSuccess = dynamicDatasource.createDataSource(dataSource);
            if(isSuccess){
                if(dataSourceDO!=null && StringUtils.isNotBlank(dataSourceDO.getKey())){
                    dynamicDatasource.delDatasources(dataSourceDO.getKey());
                }
            }else{
                throw new Exception("数据源错误");
            }
        }
        return i;
    }

    @Override
    public int batchDelete(List<DataSourceDO> dataSources) {
        for(DataSourceDO dataSource:dataSources){
          this.delete(dataSource);
        }
        return dataSources.size();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(DataSourceDO dataSource) {
        int i= this.dataSourceMapper.deleteByID(dataSource.getId());
        if(i>0){
            dynamicDatasource.delDatasources(dataSource.getKey());
        }
        return i;
    }

    private void checkKey(DataSourceDO dataSource) throws Exception{
        DataSourceDO byKey = this.dataSourceMapper.getByKey(dataSource.getKey());
        if(byKey==null){
            return;
        }
        if(!(dataSource.getKey().equals(byKey.getKey()) && dataSource.getId().equals(byKey.getId()))){
            throw new Exception("数据源key值重复");
        }
    }


}
